home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / e / newgui / src / examples / newmandel_int.e < prev    next >
Text File  |  1998-08-10  |  10KB  |  265 lines

  1. /* 
  2.  *  Runs the NewGUI-Engine (guimessage-handling) in a seperate Task
  3.  * -===============================================================-
  4.  * 
  5.  * The Code for the calculating is taken out of the Amiga-E Example-
  6.  * Sources from gfx/intmandel.e it was only changed to fit into the
  7.  * scheme of the gfx-plugin and into the task-handling-code!
  8.  */
  9.  
  10. OPT     LARGE 
  11. OPT     OSVERSION = 37
  12. OPT     PREPROCESS
  13.  
  14. MODULE  'dos/dos'                               -> For SIGBREAKF_CTRL_C
  15. MODULE  'graphics/rastport'                     -> RastPort-Structure needed for the gfx-macros
  16. MODULE  'graphics/gfxmacros'                    -> Needed for SetAFPt
  17. MODULE  'newgui/newgui'                         -> NewGUI-Engine
  18. MODULE  'newgui/ng_guitask'                     -> Task-Code for NewGUI
  19. MODULE  'newgui/ng_showerror'                   -> Error-Handling-Code (not neccesarry)
  20. MODULE  'intuition/intuition'                   -> For window.rport-PTR
  21.  
  22. ENUM    GUI_MAIN = 1,
  23.         GUI_WIN2
  24.  
  25. OBJECT  gfx     OF      plugin                  -> A Plugin to do BASIC gfx-Output to a Plugin-Area
  26.  width                                          -> the Plugins Area so the display will not be trashed
  27.  height                                         -> if a resize happens...
  28. PRIVATE                                         -> Feel free to modify it, maybe you could make it
  29.  rastport                                       -> to use a own bitmap/rastport and copy this bitmap into
  30. ENDOBJECT
  31.  
  32. CONST   GFX = PLUGIN                            -> A Synonym for PLUGIN...
  33.  
  34. DEF     info:PTR TO guitaskinfo,                -> Guitaskinfo-Structure
  35.         gfx:PTR TO gfx,                         -> BASIC Graphics-Output in a NewGUI - Window
  36.         y=0,                                    -> Starting Point for mandelbrot-generation
  37.         num_line=NIL,                           -> Calculation-Status (+ gadget-adress)
  38.         num_pixel=NIL,                          -> Calculation-Status (+ gadget-adress)
  39.         iterations=20,                          -> Iterations in the Main-calculating loop
  40.         run=TRUE,                               -> Stop/Run the Calculation
  41.         refreshgui=TRUE                         -> Refresh the Number-Display
  42.  
  43. PROC main()     HANDLE
  44.  opengui()                                      -> Open the gui (in a other task!)
  45.  
  46.   calculate()                                   -> Calculate the Mandelbrot-GFX
  47.  
  48. EXCEPT DO                                       -> Exception-Handling, but do it every time the program ends
  49.  ng_endtask(info)                               -> End and kill the gui-task
  50.  IF (gfx<>NIL) THEN END gfx                     -> And free the mem for the plugin if neccessary
  51.   IF exception THEN ng_showerror(exception)     -> If any exception happened then show it
  52.  CleanUp(exception)                             -> Cleanup (with exception as return-code)
  53. ENDPROC
  54.  
  55. PROC opengui()                                  -> Should open the gui!
  56.   info:=ng_newtask(                             -> initialize a new task for the GUI!!!!
  57.        [NG_WINDOWTITLE,         'NewGUI-Task-Demo',
  58. ->        NG_WINDOWTYPE,          WTYPE_NOSIZE,   -> No size-Gadget because the Plugin does not
  59.         NG_FILLHOOK,    {fillrect},             -> Procedure to fill the windows-back and/or groups
  60.         NG_GUIID,       GUI_MAIN,               -> a resize occours...
  61.         NG_GUI,
  62.                 [ROWS,
  63.                 [DBEVELR,
  64.                 [FILLGROUP1,
  65.                 [EQROWS,
  66.                         [TEXT,'Calculation','Mandelbrot',FALSE,2],
  67.                         [TEXT,'a seperate Task','The GUI runs in',FALSE,2],
  68.                         [TEXT,'NewGUI','with',FALSE,2]
  69.                 ]]],
  70.                 [BEVELR,
  71.                 [FILLGROUP1,
  72.                 [ROWS,
  73.                         [GFX,0,NEW gfx.gfx(200,100)]    -> The Plugin...
  74.                 ]]],
  75.                 [DBEVELR,
  76.                 [FILLGROUP1,
  77.                 [ROWS,
  78.                 [EQCOLS,
  79.                         num_line:=[NUM,0,'Line',TRUE,3],
  80.                         num_pixel:=[NUM,0,'Pixel',TRUE,3]
  81.                 ],
  82.                         [CHECK,{refresh},'Display Render-Info',refreshgui,TRUE],
  83.                 [BEVELR,
  84.                 [COLS,
  85.                         [TEXT,'Iterations :','Number of',FALSE,3],
  86.                         [SLIDE,{setiteration},'     ',FALSE,1,50,iterations,2,'%2ld']
  87.                 ]]]]],
  88.                 [BAR],
  89.                 [BEVELR,
  90.                 [FILLGROUP1,
  91.                 [EQCOLS,
  92.                         [SBUTTON,{stop},'Stop'],
  93.                 [SPACEH],
  94.                         [SBUTTON,{reset},'Reset']
  95.                 ]]]],
  96.         NIL,NIL],NG_STACK_SIZE)                 -> NG_STACK_SIZE is Standart-Size of 4096 bytes!
  97. ENDPROC
  98.  
  99. PROC calculate()                                -> Do other important things...
  100.  DEF    zr,zi,ar,ai,dr,di,sr,si,st,x,i,
  101.         xsize=0,ysize=0
  102.   Wait(info.sig OR SIGBREAKF_CTRL_C)            -> Wait until the GUI is ready (or Break!)
  103.    WHILE (ng_checkgui(info)=FALSE)              -> Wait until the GUI will be closed
  104.     IF (y=0)
  105.      xsize:=gfx.width
  106.       ysize:=gfx.height
  107.        sr:=$400000/xsize
  108.        si:=$300000/ysize
  109.       st:=$140000*-2
  110.      zi:=$160000
  111.     ENDIF
  112.     IF (run=TRUE) AND (y<gfx.height)
  113.      IF (xsize<>gfx.width) OR (ysize<>gfx.height)
  114.       xsize:=gfx.width
  115.        ysize:=gfx.height
  116.         sr:=$400000/xsize
  117.         si:=$300000/ysize
  118.        st:=$140000*-2
  119.       zi:=$160000
  120.      ENDIF
  121.        zi:=zi-si
  122.         zr:=st
  123.          FOR x:=0 TO xsize-1
  124.           i:=0
  125.            ar:=zr
  126.             ai:=zi
  127.              REPEAT
  128.               dr:=Shr(ar,10)
  129.                di:=Shr(ai,10)
  130.                 ai:=dr*2*di+zi
  131.                  dr:=dr*dr
  132.                 di:=di*di
  133.                ar:=dr-di+zr
  134.               i++
  135.              UNTIL (i>iterations) OR (dr+di>$400000)
  136.             IF (refreshgui=TRUE) THEN ng_setattrsA([
  137.                 NG_GUI,         info.gui,
  138.                 NG_CHANGEGAD,   TRUE,
  139.                 NG_GADGET,      num_pixel,
  140.                 NG_NEWDATA,     x,
  141.                 NIL,            NIL])
  142.            gfx.plot(x,y,Mod(i,16))
  143.           zr:=zr+sr
  144.          ENDFOR
  145.         IF (refreshgui=TRUE) THEN ng_setattrsA([
  146.                 NG_GUI,         info.gui,
  147.                 NG_CHANGEGAD,   TRUE,
  148.                 NG_GADGET,      num_line,
  149.                 NG_NEWDATA,     y,
  150.                 NIL,            NIL])
  151.       IF Odd(y) THEN Delay(1)
  152.      y++
  153.     ELSE
  154.      Delay(1)
  155.     ENDIF
  156.    ENDWHILE
  157. ENDPROC
  158.  
  159. PROC stop()                                     -> Stop/Run the GUI...
  160.  IF (run=TRUE)
  161.   run:=FALSE
  162.  ELSE
  163.   run:=TRUE
  164.  ENDIF
  165. ENDPROC
  166.  
  167. PROC refresh(x,y)                       IS refreshgui:=y
  168.  
  169. PROC setiteration(x,y)                  IS iterations:=y
  170.  
  171. PROC reset()
  172.  run:=FALSE
  173.   gfx.clear()
  174.   y:=0
  175.  run:=TRUE
  176. ENDPROC
  177.  
  178. PROC fillrect(rp,x,y,width,height,type)
  179.  DEF    oldbpen=0,
  180.         oldapen=1
  181.   SELECT        type
  182.         CASE    NG_FILL_WINDOW                          -> Window-Filling (Back)
  183.          oldbpen:=SetBPen(rp,0)                         -> Set Backpen to gray
  184.           oldapen:=SetAPen(rp,3)                        -> Set Frontpen to blue
  185.            SetAfPt(rp,[$AAAA,$5555]:INT,1)              -> Set Pattern (ATTENTION! Macro-Definition in gfxmacros.m, this need OPT PREPROCESS!!!)
  186.             RectFill(rp,x,y,width,height)               -> Now fill the Region!
  187.            SetBPen(rp,oldbpen)                          -> Set the Backpen to the old value
  188.           SetAPen(rp,oldapen)                           -> Set the Frontpen to the old value
  189.         CASE    FILLGROUP1                              -> Fill the Group 1
  190.          oldbpen:=SetBPen(rp,0)                         -> Set BackPen to gray
  191.           oldapen:=SetAPen(rp,0)                        -> Set Frontpen to gray
  192.            SetAfPt(rp,[$FFFF,$FFFF]:INT,1)              -> ...
  193.             RectFill(rp,x,y,width,height)               -> ...
  194.            SetBPen(rp,oldbpen)                          -> ...
  195.           SetAPen(rp,oldapen)                           -> ...
  196.    ENDSELECT
  197. ENDPROC
  198.  
  199. -> Plugin-Stuff (Not very Flexible at all, because the Display is trashed at resize
  200. -> The problem could be solved with making a temporary rastport and then Copy this
  201. -> rastport into the Plugins-Box... feel free ! :-)
  202.  
  203. PROC gfx(minwidth,minheight)            OF gfx
  204.  self.width:=minwidth
  205.   self.height:=minheight
  206. ENDPROC
  207.  
  208. PROC will_resize()                      OF gfx IS RESIZEXANDY
  209.  
  210. PROC min_size(x,y)                      OF gfx IS self.width, self.height
  211.  
  212. PROC render(ta,x,a,xs,ys,win:PTR TO window)     OF gfx
  213.  IF (self.rastport=NIL) THEN self.rastport:=win.rport
  214.   run:=FALSE
  215.    y:=0
  216.     self.width:=xs
  217.     self.height:=ys
  218.    self.clear()
  219.   run:=TRUE
  220. ENDPROC
  221.  
  222. PROC clear()                            OF gfx  
  223.  SetAPen(self.rastport,0)
  224.   RectFill(self.rastport,self.x,self.y,(self.x+self.xs),(self.y+self.ys))
  225. ENDPROC
  226.  
  227. PROC plot(x,y,color=1)                  OF gfx
  228.  IF (self.rastport<>NIL)
  229.   SetStdRast(self.rastport)
  230.    IF (x<self.xs)
  231.     IF (y<self.ys)
  232.      Plot(x+self.x,y+self.y,color)
  233.      RETURN TRUE
  234.     ENDIF
  235.    ENDIF
  236.   ENDIF
  237. ENDPROC FALSE
  238.  
  239. PROC line(x1,y1,x2,y2,color=1)          OF gfx
  240.  IF (self.rastport<>NIL)
  241.   SetStdRast(self.rastport)
  242.    IF (x1<self.xs) AND (x2<self.xs)
  243.     IF (y1<self.ys) AND (y2<self.ys)
  244.      Line(x1+self.x,y1+self.y,x2+self.x,y2+self.y,color)
  245.      RETURN TRUE
  246.     ENDIF
  247.    ENDIF
  248.  ENDIF
  249. ENDPROC FALSE
  250.  
  251. PROC box(x1,y1,x2,y2,color=1)           OF gfx
  252.  IF (self.rastport<>NIL)
  253.   SetStdRast(self.rastport)
  254.    IF (x1<self.xs) AND (x2<self.xs)
  255.     IF (y1<self.ys) AND (y2<self.ys)
  256.      Box(x1+self.x,y1+self.y,x2+self.x,y2+self.y,color)
  257.      RETURN TRUE
  258.     ENDIF
  259.    ENDIF
  260.  ENDIF
  261. ENDPROC FALSE
  262.  
  263. PROC colour(foreground,background=0)    OF gfx IS Colour(foreground,background)
  264.  
  265.